package com.woniuxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.pojo.Doctor;
import com.woniuxy.pojo.Result;
import com.woniuxy.pojo.dto.BatchStatusDTO;
import com.woniuxy.pojo.dto.DoctorDTO;
import com.woniuxy.pojo.dto.SearchDoctorDTO;
import com.woniuxy.pojo.vo.DoctorVO;
import com.woniuxy.service.DoctorService;
import com.woniuxy.service.HospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("doctor")
@Api(tags = "医生管理接口")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private HospitalService hospitalService;
    @GetMapping("/getDoctor/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取医生数据", notes = "根据页码和每页数量获取医生列表")
    public Result getDoctors(@PathVariable("pageSize") Integer pageSize, @PathVariable("pageNum") Integer pageNum){
        Page<DoctorVO> doctors = doctorService.getDoctors(pageNum, pageSize);
        return Result.ok(doctors);
    }
    @GetMapping("/getDartment")
    @ApiOperation(value = "获取一级科室信息")
    public Result   getDartment(){
        List<String> dartments = doctorService.getDartment();
     return Result.ok(dartments);
    }
    @PostMapping("/batchDeleteDoctor")
    @ApiOperation(value = "批量删除医生")
    public Result batchDeleteDoctor(@RequestBody List<Integer> ids){
        boolean b = doctorService.removeByIds(ids);
        return b?Result.ok():Result.fail();
    }
    @PostMapping("/updateStatus")
    @ApiOperation("更新医生状态")
    public Result updateStatus(@RequestBody DoctorDTO dto){
        Doctor doctor = doctorService.getById(dto.getId());
        if (doctor == null) {
            return Result.fail("医生不存在");
        }
        doctor.setStatus(dto.getStatus());
        boolean success = doctorService.updateById(doctor);
        return success ? Result.ok() : Result.fail("状态更新失败");
    }
    @PostMapping("/batchUpdateStatus")
    @ApiOperation("批量更新医生状态")
    public Result batchUpdateStatus(@RequestBody BatchStatusDTO dto){
        if (dto.getIds() == null || dto.getIds().isEmpty()) {
            return Result.fail("请选择要操作的医生");
        }
        boolean success = doctorService.lambdaUpdate()
                .in(Doctor::getId, dto.getIds())
                .set(Doctor::getStatus, dto.getStatus())
                .update();
                return success ? Result.ok() : Result.fail("批量操作失败");
    }
    @PostMapping("/searchDoctor/{pageNum}/{pageSize}")
    @ApiOperation(value = "搜索医生")
    public Result<Page<DoctorVO>> searchDoctor(
            @RequestBody SearchDoctorDTO queryDTO,  // 使用专用DTO
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize){
        Page<DoctorVO> page = doctorService.searchDoctor(queryDTO, pageNum, pageSize);
        return Result.ok(page);
    }

    @GetMapping("/getDoctorById/{id}")
    @ApiOperation(value = "根据id查询医生")
    public Result<Doctor> getDoctorById(@PathVariable("id") Integer id){
        Doctor doctor = doctorService.getById(id);
        return Result.ok(doctor);
    }
    @GetMapping("/getTitle")
    @ApiOperation(value = "获取职称")
    public Result<List<String>> getTitle(){
        List<String> titles =doctorService.getTitle();
        return Result.ok(titles);
    }
    @GetMapping("/getHospital")
    @ApiOperation(value = "获取医院名称")
    public Result<List<String>> getHospital(){
        List<String> hospitals = hospitalService.getHospital();
        return Result.ok(hospitals);
    }
    @PostMapping("/addDoctor")
    @ApiOperation(value = "添加医生")
    public Result<Doctor> addDoctor(@RequestBody Doctor doctor){
        boolean save = doctorService.save(doctor);
        return save?Result.ok(doctor):Result.fail();
    }

}
